package cn.zlp.org.service.impl;

import cn.zlp.basic.service.impl.BaseServiceImpl;
import cn.zlp.basic.util.FastDfsUtil;
import cn.zlp.basic.util.MD5Utils;
import cn.zlp.basic.util.StrUtils;
import cn.zlp.org.domain.Department;
import cn.zlp.org.domain.Employee;
import cn.zlp.user.domain.Logininfo;
import cn.zlp.org.mapper.EmployeeMapper;
import cn.zlp.user.mapper.LogininfoMapper;
import cn.zlp.org.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工Service类,先继承公共的Service类,再实现接口
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Autowired(required = false)
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee) {
        // 获取随机32位盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 设置用户的用户盐值
        employee.setSalt(salt);
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        // 设置用户的加盐加密密码
        employee.setPassword(password);
        if (employee.getDepartment() != null){ // 获取员工的部门信息,不为null,说明为平台员工
            // 设置登录信息的用户所在部门
            Department department = employee.getDepartment();
            // 设置登录信息的用户所在部门id
            employee.setDepartment_id(department.getId());
        }
        // 调用方法employee2Logininfo,将employee转换为Logininfo对象
        Logininfo logininfo = employee2Logininfo(employee);
        // 设置为平台员工
        logininfo.setType(0);
        // 添加登录信息
        logininfoMapper.save(logininfo);
        // 获取登录信息的自增长主键,赋值给登录信息id
        employee.setLogininfo_id(logininfo.getId());
        // 添加最新的用户
        employeeMapper.save(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = employee.getLogininfo();
        if (employee.getState() == 0){ // 获取员工的状态,然后对登录信息设置
            logininfo.setDisable(false);  // 禁用
        } else {
            logininfo.setDisable(true);  // 启用
        }
        // 将两个对象的相同属性进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        return logininfo;
    }

    @Override
    public void update(Employee employee) {
        // 获取员工的部门信息
        Department department = employee.getDepartment();
        // 设置登录信息的用户所在部门id
        employee.setDepartment_id(department.getId());
        String password = MD5Utils.encrypByMd5( employee.getPassword() + employee.getSalt());
        // 设置用户的加盐加密密码
        employee.setPassword(password);
        // 调用方法employee2Logininfo,将employee转换为Logininfo对象
        Logininfo logininfo = employee2Logininfo(employee);
        // 因为上面转换的时候将id页也一起转换了,所以需要重新赋值
        logininfo.setId(employee.getLogininfo().getId());
        // 修改登录信息
        logininfoMapper.update(logininfo);
        // 修改用户信息
        employeeMapper.update(employee);
    }

    @Override
    public List<Employee> findAdmin() {
        // 查询所有店铺管理员
        return employeeMapper.findAdmin();
    }

    @Override
    public void del(Long id) {
        // 传过来的是id,我们要获取登录信息id,然后将登录信息一并删除了
        Employee employee = employeeMapper.loadById(id);
        if (employee.getShop() != null) { // 判断是否有店铺信息
            // 获取店铺的图标
            String logo = employee.getShop().getLogo();
            // 取出第一 /
            String pathTmp = logo.substring(1); // goup1/xxxxx/yyyy
            // 截取图标路径的组
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            // 截取图标路径的文件名
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            // 删除服务器中的图标
            FastDfsUtil.delete(groupName,remotePath);
        }
        // 删除登录信息
        logininfoMapper.delete(employee.getLogininfo_id());
        // 删除用户
        employeeMapper.delete(id);
    }
}
